class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if(nums.size()==0) return 0;
        if(nums.size()==1) return 1;
        sort(nums.begin(),nums.end());
        int cur=1;
        int max_lenth=0;
        int lenth=1;
        while(cur<nums.size()){
            if(nums[cur-1]+1==nums[cur]){
                lenth++;
                max_lenth=max(lenth,max_lenth);
                while(cur+1<nums.size()&&nums[cur+1]==nums[cur]) cur++;
            }
            else lenth=1;
            cur++;
        }
        max_lenth=max(lenth,max_lenth);
        return max_lenth;
    }
};